package server;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server {
	/*
	this is a server which can connects with many clients(max is 15) and
	show the file list or send a file to a client
	 */
	public static void main(String[] args) throws IOException {
		/*
		run the server
		 */

		ServerSocket server = null;
		ExecutorService service;

		// Try to open up the listening port
		try {
			server = new ServerSocket(8888);
		} catch (Exception e) {
			System.err.println("Could not listen on port: 8888.");
			System.exit(-1);
		}

		// Initialise the executor.
		service = Executors.newFixedThreadPool(15);

		// For each new client, submit a new handler to the thread pool.
		while( true ) {
			Socket client = server.accept();
			service.submit( new ClientHandler(client) );
		}
	}
}